#*******************************************************************
# Copyright (c)2016-2020, ShenZhen WanJiaAn Technology CO.,LTD.
# All Rights Reserved.
#*******************************************************************
CHIP_TYPE := HI3518EV300 #HI3518EV200 HI3518EV300

CROSS = arm-himix100-linux-
CC    = $(CROSS)gcc
CPP   = $(CROSS)g++
AR    = $(CROSS)ar
STRIP = $(CROSS)strip

APPROOT := .

#编译产生中间文件的保存路径
ifeq ($(debug), 1)
    INTERMEDIATE = $(APPROOT)/out/intermediate_debug
else
    INTERMEDIATE = $(APPROOT)/out/intermediate
endif

#发布的目标文件
TARGETDIR  = $(APPROOT)/out
TARGETNAME = ipcam
ifeq ($(debug), 1)
    DIST_BIN := $(TARGETDIR)/$(TARGETNAME)_debug
else
    DIST := $(TARGETDIR)/$(TARGETNAME)
    DIST_BIN := $(DIST).bin
    DIST_MAP := $(DIST).map
    DIST_BIN_ZIP := $(DIST).bin.zip
endif

ifeq ($(debug), 1)
    CFLAGS += -g -O0
else
    CFLAGS += -O2
endif

#编译参数及指定头文件
CFLAGS += -Wall -W

#链接本地库
#LDLIBS += \
		  $(APPROOT)/Lib/libfun1.a \
		  $(APPROOT)/Lib/libfun2.a 
		  
#链接系统库
#LFLAGS +=  -lpthread -lm -lrt

include $(APPROOT)/build/config.make
include $(APPROOT)/build/SrcFileList
include $(APPROOT)/build/Makefile.param
include $(APPROOT)/build/liteos.mak
include $(APPROOT)/build/Modules.mak
CFLAGS += -D${CHIP_TYPE}

#################### 源文件转换成.o ####################
TMPOBJS_gbl = $(patsubst $(APPROOT)/%.cpp, $(INTERMEDIATE)/%.o, $(SRC_gbl))
OBJS_gbl = $(patsubst $(APPROOT)/%.c, $(INTERMEDIATE)/%.o, $(TMPOBJS_gbl))

TMPOBJS_modules = $(patsubst $(APPROOT)/%.cpp, $(INTERMEDIATE)/%.o, $(SRC_modules))
OBJS_modules = $(patsubst $(APPROOT)/%.c, $(INTERMEDIATE)/%.o, $(TMPOBJS_modules))

#################### 源文件转换成.d ####################
OBJS := $(OBJS_gbl) $(OBJS_modules)
OBJD := $(patsubst %.o, %.d, $(OBJS))

#################### 指定需生成的模块静态库 ####################
LIBAPP_modules := $(INTERMEDIATE)/libmodules.a
LIBAPP_SUM := $(LIBAPP_modules)

.PHONY: all print clean

all: $(LIBAPP_SUM) $(OBJD) $(DIST_BIN) 
	@echo dist: $(DIST_BIN_ZIP)
	@echo -e "\e[0;32;1m--Output '$(DIST_BIN_ZIP)' finished\e[0;36;1m"
	@echo -e "\e[0m"

$(DIST_BIN): $(DIST)
	@$(OBJCOPY) -O binary $(DIST) $(DIST_BIN)
	@-cd $(APPROOT)/tools;./hi_gzip ../$(DIST_BIN) ../$(DIST_BIN_ZIP)
	
$(DIST): $(OBJS_gbl) $(LIBAPP_SUM) $(LDLIBS) 
	@mkdir -p $(dir $@)
	@echo here?
	$(LD) $(LDFLAGS) -Map=$(DIST_MAP) -o $(DIST) --start-group $^ --end-group $(LFLAGS) $(SDK_LIB) $(LITEOS_LIBDEPS) 
	@$(OBJDUMP) -d $(DIST) > $(DIST).asm

$(INTERMEDIATE)/%.o: $(APPROOT)/%.c
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -c $< -o $@

$(INTERMEDIATE)/%.o: $(APPROOT)/%.cpp
	@mkdir -p $(dir $@)
	$(CPP) $(CFLAGS) -c $< -o $@

$(INTERMEDIATE)/%.d: $(APPROOT)/%.c
	@mkdir -p $(dir $@)
	@rm -f $@
	$(CC) $(CFLAGS) -MM -MD $< -o $@
	@sed 's/.*\.o/$(subst /,\/, $(dir $@))&/g' $@ >$@.tmp
	@mv $@.tmp $@

$(INTERMEDIATE)/%.d: $(APPROOT)/%.cpp
	@mkdir -p $(dir $@)
	@rm -f $@
	$(CPP) $(CFLAGS) -MM -MD $< -o $@
	@sed 's/.*\.o/$(subst /,\/, $(dir $@))&/g' $@ >$@.tmp
	@mv $@.tmp $@

#判断是否为clean print,若是则不生成.d
ifeq ($(filter $(MAKECMDGOALS), clean print),)
sinclude $(OBJD)
endif

print:
	@echo -e "\033[33m"
	@echo $(INTERMEDIATE)
	@echo $(TARGETDIR)
	@echo DIST: $(DIST)
	@echo "################ CFLAGS ################"
	@echo $(CFLAGS)
	@echo "################ LFLAGS ################"
	@echo $(LFLAGS)
	@echo "################ LDLIBS ################"
	@echo $(LDLIBS)
	@echo "################ LIBAPP_SUM ################"
	@echo $(LIBAPP_SUM)
	@echo "################ OBJS ################"
	@echo $(OBJS)
	@echo -e "\033[0m"

clean:
	find $(INTERMEDIATE)/ -type f -name 'core' -print | xargs rm -f
	find $(INTERMEDIATE)/ -type f -name '*tags' -print | xargs rm -f
	find $(INTERMEDIATE)/ -type f -name '*.bak' -print | xargs rm -f
	find $(INTERMEDIATE)/ -type f -name '*~' -print | xargs rm -f
	find $(INTERMEDIATE)/ -type f -name '.depend' -print | xargs rm -f
	find $(INTERMEDIATE)/ -type f -name '*.o'  -print | xargs rm -f
	find $(INTERMEDIATE)/ -type f -name '*.d'  -print | xargs rm -f
	find $(INTERMEDIATE)/ -type f -name '*.a'  -print | xargs rm -f
	find $(APPROOT)/ -type f -name '*.tmp'  -print | xargs rm -f
	rm -rf $(DIST)
	rm -rf $(DIST).asm
	rm -rf $(DIST).map
	rm -rf $(DIST).bin
	rm -rf $(DIST).bin.zip

#模块静态库生成
$(LIBAPP_modules): $(OBJS_modules)
	$(AR) crus $@ $^
	@echo -e "\e[0;32;1m--Compile '$@' finished\e[0;36;1m"
	@echo -e "\e[0m"

